﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace winformcontrol
{
    public partial class winformgdictrl : UserControl
    {
        public winformgdictrl()
        {
            InitializeComponent();

            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);

            m_arraylines = new ArrayList();
            m_bdrawing = false;
        }

        private ArrayList m_arraylines;
        private bool m_bdrawing;
        //m_arraylines为线对象集合，m_bdrawing指示是否画线。

        private void winformgdictrl_mousedown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            lineobj m_lineobj = new lineobj(e.X, e.Y);
            m_arraylines.Add(m_lineobj);
            m_bdrawing = true;
        }

        private void winformgdictrl_mousemove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (m_bdrawing)
            {
                lineobj m_lineobj = (lineobj)m_arraylines[m_arraylines.Count - 1];
                m_lineobj.m_endpoint = new Point(e.X, e.Y);
                this.Invalidate();
            }
        }

        private void winformgdictrl_mouseup(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            m_bdrawing = false;
        }

        private void winformgdictrl_paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.FillRectangle(Brushes.Yellow, this.ClientRectangle);

            foreach (object obj in m_arraylines)
            {
                lineobj m_lineobj = (lineobj)obj;
                m_lineobj.draw(g);
            }
        }

    }
}
